package com.baselibrary.utils.emo;

import android.content.Context;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author: 关忆北、
 * @date: 2022/4/23
 * @Des：
 */
public class EmojiUtils {

    /**
     * 从assets目录下获取所有表情
     *
     * @return
     */
    public static String[] getEmojis(Context mContext) {
        BufferedReader br = null;
        String emojis[] = null;
        try {
            InputStream is = mContext.getAssets().open("emoji.json");
            StringBuffer sb = new StringBuffer();
            br = new BufferedReader(new InputStreamReader(is));
            String line = null;
            while (null != (line = br.readLine())) {
                sb.append(line).append("\r\n");
            }
            //解析数据，也可以用gson解析，这里只是用其中一种
            JSONArray emojiArray = new JSONArray(sb.toString());
            if (null != emojiArray && emojiArray.length() > 0) {
                emojis = new String[emojiArray.length()];
                for (int i = 0; i < emojiArray.length(); i++) {
                    emojis[i] = emojiArray.optString(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return emojis;
    }

    /**
     * 获取常用表情
     */

    public static String[] getOftenEmo(Context mContext) {
        String[] oftenArray =  new String[6];
        String[] emojis = getEmojis(mContext);
        for (int i = 0; i < 6; i++) {
            oftenArray[i] = emojis[i];
        }
        return oftenArray;
    }
}
